/**
as3app - An open-source ActionScript 3.0 Library developed to assist 
in creating simple visual controls/data binding/asset loading for the
Flash player platform.

http://code.google.com/p/as3app/

Copyright (c) 2011 M. K. Chowdhury, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package as3app.dataContext 
{
	/**
	 * ...
	 * @author Muntasir Kabir Chowdhury
	 */
	public class DataContext extends Context
	{
		private static var quickRout:Array = new Array();
		private static var numInstance:int = 1;
		private static var instance:DataContext = null;
		
		
		
		/**
		 * Dont instantiate the class. Use static methods.
		 */
		public function DataContext() 
		{
			if (numInstance > 0) throw new Error("Cant create instance of : com.funrock.util.dataContext.DataContext"); 
			numInstance = 1;
		}
		
		
		
		
		
		
		
		
		/**
		 * Initiates class
		 */
		private static function Init()
		{
			if (instance == null)
			{
				numInstance = 0;
				instance = new DataContext();
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * Create a new context by given name.
		 * @param	name	Context name
		 * @param	value	
		 */
		public static function Create(name:String, value:*)
		{
			Init();
			
			var data:* = quickRout[name];
			if (data == null) 
			{
				data = instance.Resolve(name, true);
				quickRout[name] = data;
			}
			
			if (data != null) 
			{
				var container:DataContainer = data.Container as DataContainer;
				container.Update(data.Name, value);
			}
		}
		
		
		
		
		
		
		/**
		 * Update context value by given name.
		 * @param	name	Context name
		 * @param	value
		 */
		public static function Update(name:String, value:*)
		{
			Init();
			var data = quickRout[name];
			if (data != null) 
			{
				var container:DataContainer = data.Container as DataContainer;
				container.Update(data.Name, value);
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * Subscribe to a context to change notification.
		 * @param	name	Context name
		 * @param	handler
		 */
		public static function Subscribe(name:String, handler:Function)
		{
			Init();
			var data = quickRout[name];
			if (data != null) 
			{
				var container:DataContainer = data.Container as DataContainer;
				container.Subscribe(data.Name, handler);
			}
		}
		
		
		
		
		
		
		
		/**
		 * Unsubscribe from a context from getting change notification.
		 * @param	name	Context name
		 * @param	handler		Function :: function(oldVal:*,newVal:*):void
		 */
		public static function UnSubscribe(name:String, handler:Function)
		{
			Init();
			var data = quickRout[name];
			if (data != null) 
			{
				var container:DataContainer = data.Container as DataContainer;
				container.UnSubscribe(data.Name, handler);
			}
		}
		
		
		
		
		
		
		
		/**
		 * Get context value.
		 * @param	name	Context name
		 * @return
		 */
		public static function Get(name:String):*
		{
			Init();
			var data = quickRout[name];
			
			if (data == null) return null;
			var container:DataContainer = data.Container as DataContainer;
			return (container != null?container.Value(data.Name):null);
		}
		
		
		
		
		/**
		 * Has context with the given name?
		 * @param	name	Context name
		 * @return Boolean
		 */
		public static function Has(name:String):Boolean
		{
			Init();
			var data = quickRout[name];
			return data != null?data.Container.Has(data.Name):false;
		}
		
		
	}

}